<template>
  <el-dialog title="新增用户" :close-on-click-modal="false" @close="close" :visible.sync="visible" top="10%">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="头像" :label-width="formLabelWidth" prop="icon">
        <c-avatar-upload v-model="form.icon" size="small"></c-avatar-upload>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="userName">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="realName">
            <el-input v-model="form.realName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机" :label-width="formLabelWidth" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生日" :label-width="formLabelWidth" prop="birthday">
            <el-date-picker
              v-model="form.birthday"
              @change="v => form.birthday=v"
              type="date"
              placeholder="出生日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="email" :label-width="formLabelWidth" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="QQ" :label-width="formLabelWidth" prop="qq">
            <el-input v-model="form.qq"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="微信" :label-width="formLabelWidth" prop="wx">
            <el-input v-model="form.wx"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" :label-width="formLabelWidth" prop="departIds">
            <c-select v-model="form.departIds" type="Department" placeholder="请选择"></c-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色" :label-width="formLabelWidth" prop="roleIds">
            <c-select v-model="form.roleIds" type="RoleName" multiple placeholder="请选择"></c-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <c-button type="primary" @click="btnOk">确 定</c-button>
    </div>
  </el-dialog>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'SDialogAdd',
  props: {},
  data () {
    return {
      formLabelWidth: '120px',
      visible: true,
      form: {
        icon: '',
        userName: '',
        password: '',
        realName: '',
        birthday: '',
        mobile: '',
        email: '',
        qq: '',
        wx: '',
        departIds: '',
        roleIds: [],
        positionLevel: '',
        identityCode: '1'
      },
      rules: {
        userName: this.$checkRules.username,
        password: this.$checkRules.password,
        realName: this.$checkRules.name,
        mobile: [
          ...this.$checkRules.mobileR,
          ...this.$checkRules.mobile
        ],
        email: [
          ...this.$checkRules.email,
          ...this.$checkRules.emailR
        ],
        departIds: this.$checkRules.department,
        roleIds: this.$checkRules.roleOption,
        positionLevel: this.$checkRules.positionLevel
        // identityCode: [{validator: validateIdentityCode, trigger: 'blur,change'}]
      }
    }
  },
  computed: {
    ...mapState('options', ['options'])
  },
  methods: {
    // 固定写法，每次加载都需要重置form
    close () {
      this.$emit('close')
    },
    btnOk (btn) {
      this.$refs['form'].validate((valid) => {
        // 校验通过
        if (valid) {
          this.$service.system.addUser(this.form).then(() => {
            this.visible = false
            this.$emit('ok')
          }).finally(() => {
            btn.isLoading(false)
          })
        } else {
          btn.isLoading(false)
        }
      })
    }
  }
}
</script>

<style>
</style>
